package cjgl.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import java.sql.PreparedStatement;

import cjgl.entity.Grade;
import cjgl.entity.TbClass;


//����
public class TbClassDAO {
	public synchronized void insert(TbClass bj) {
		String sql="insert into tbclass values(null,?,?)";
		Connection conn=ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pstmt;
			try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,bj.getName());
		    pstmt.setString(2,bj.getManager());
			pstmt.execute();
			sql="select  *from tbclass t order by t.id desc";
			ResultSet rs = pstmt.executeQuery(sql);
			rs.next();
			bj.setId(rs.getInt(1));
			conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	//�޸�
	public void update(TbClass tbclass) {
		String sql="update tbClass set name=?,manager=? where id=?";
		Connection conn=ConnectionFactory.getConnection();
		if(conn!=null) {
			PreparedStatement pstmt;
			try {
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,tbclass.getName());
				pstmt.setString(2,tbclass.getManager());
				pstmt.setInt(3, tbclass.getId());
				pstmt.execute();
				conn.close();
			} catch (SQLException e) {				
				e.printStackTrace();
			}			
		}
	    }
		
		
		
		

	
	//ɾ��
	/*public void delete(Integer id) {
		String sql="delete from tbclass t where t.id="+id;
		Connection conn=ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				stmt.execute(sql);
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
	}
	*/
	public void delete(Integer id) {
		String sql="delete from tbClass  where id="+id;
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				stmt.execute(sql);
				conn.close();
			}catch(SQLException e) {
				e.printStackTrace();
			}
		}
	}

	//��
	public TbClass findById(Integer id){
		TbClass tbClass=null;
		String sql="select * from tbclass t where t.id="+id;
		Connection conn =  ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement statement;
			try {
				statement  = conn.createStatement();
				ResultSet rs = statement.executeQuery(sql);
				if(rs.next()) {
					tbClass= new TbClass(rs.getInt(1), rs.getString(2), rs.getString(3));
				}
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return tbClass;
	}
	//list
	public List<TbClass> findAll(){
		List<TbClass> list= new ArrayList<TbClass>();
		String sql="select * from tbClass";
		Connection conn = ConnectionFactory.getConnection();
		if(conn!=null) {
			Statement stmt;
			try {
				stmt = conn.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				while(rs.next()) {
					TbClass bj=new TbClass(rs.getInt(1), rs.getString(2), rs.getString(3));
					list.add(bj);
				}
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
public static void main(String[] args) {
	TbClassDAO classDAO=new TbClassDAO();
	ObjectMapper mapper=new ObjectMapper();
//	List<TbClass> list=classDAO.findAll();
	TbClass bj=classDAO.findById(2);
	try {
		String str=mapper.writeValueAsString(bj);
		System.out.println(str);
	} catch (JsonGenerationException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (JsonMappingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	//List<TbClass>list = classDAO.findAll();
	//System.out.println(list);
	//TbClass bj=classDAO.finById(1);
	//System.out.println(bj);
	//TbClass bj=new TbClass(null,"133333","����ʦ");
	//classDAO.insert(bj);
	//TbClass bj1=classDAO.findById(2);
    //System.out.println(bj1);
 }
}